package com.example.shipload_e.update_tool;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;

public class NetworkUtil {

	public static boolean checkNetworkState(final Context activity) {
		try {

			// 判断有没有网
			ConnectivityManager manager = (ConnectivityManager) activity
			        .getSystemService(Context.CONNECTIVITY_SERVICE);
			NetworkInfo activeNetworkInfo = manager.getActiveNetworkInfo();
			if(activeNetworkInfo == null) {
				AlertDialog.Builder dialog = new Builder(activity);

				// 没网显示一个dialog
				dialog.setMessage("亲，现在没网");
				// 打开网络
				dialog.setPositiveButton("打开", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog,int which) {
						// 每个手机已经有一个activity界面打开网络
						// <intent_filter><action>open
						Intent intent = new Intent(android.provider.Settings.ACTION_WIRELESS_SETTINGS);
						activity.startActivity(intent);
						dialog.cancel();
					}
				});

				dialog.setNegativeButton("取消", new OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog,int which) {
						dialog.cancel();

					}
				});
				dialog.show();
			} else {
				NetworkInfo wifiNetworkInfo = manager.getNetworkInfo(manager.TYPE_WIFI);
				if(wifiNetworkInfo != null && wifiNetworkInfo.isConnected()) {

					return true;
				} else {
					return false;
				}
			}
		} catch (Exception e) {

		}
		return false;
	}

}
